<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML lang=en xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
	<TITLE>DoctorDoc | Edit </TITLE>
	<META http-equiv=Content-Type content="text/html; charset=UTF-8" />
</HEAD>
<BODY>

<?php
include("nav.php");
?>

<?php


/* Connect to database */
$con = mysql_connect('drdoc2.cfcbem1draxy.us-east-1.rds.amazonaws.com:3306', 'DrDocRoot', 'DrDoc5m4ll');
if (!$con) {
         die ('Error: Could not connect to mySQL database');
}
$mysql = "DrDocSmall";
if (!mysql_select_db($mysql)) {
        die ('Error: Could not connect to mySQL database');
}

/* Get table name from URL */
$tableName = /*"EducationDegrees";*/$_GET["data"];


// Make sure table exist in database 
if ($tableName != "") {
// Solution from http://stackoverflow.com/questions/1525784/mysql-check-if-a-table-exists-without-throwing-an-exception 
$result = mysql_query("SHOW TABLES LIKE '$tableName'");
$tableExists = mysql_num_rows($result) > 0;
} else {
$tableExits = 0;
}

// If table exists 
if ($tableExists) { //START MASTER IF


echo "<h2>Edit Data | $tableName</h2>\n";
echo "<form action='dbEduDe.php?data=$tableName' method='post'>\n";
echo "<table>\n";


/* Input forms */
$result = mysql_query("SELECT * FROM $tableName");
$fields = mysql_num_fields($result);
for ($i=0; $i < $fields; $i++) {
	if ($i % 2 == 0) {
		if ($i != 0) {
			echo "</tr>\n";
		}
		echo "<tr>";
	}
	
	$name  = mysql_field_name($result, $i);
	echo "<td>" . $name . "</td><td><input type='text' name='add_" . $name ."' /></td>\n";
	    
}
if ($field % 2 == 0) {
	echo "</tr>\n";
} else echo "<td></td><td></td><tr>\n";
echo "</table>\n";
echo "<input name='SaveChanges' value='Save Changes' type='submit' />\n";
echo "</form>\n";
echo "<br />\n";





/* INSERT VALUES INTO DATABASE */
if (count($_POST)!=0 && $_POST['SaveChanges']=='Save Changes') { 
echo "Adding...\n";
/* Building sql String */
$sql = "INSERT INTO $tableName (";
for ($i=0; $i < $fields; $i++) {
	if ($i!=0) {
		$sql = $sql . ", ";
	}
	$name  = mysql_field_name($result, $i);
	$sql = $sql . $name;
}
$sql = $sql . ") VALUES (";
for ($i=0; $i < $fields; $i++) {
        if ($i!=0) {
                $sql = $sql . ", ";
        }
        $name  = mysql_field_name($result, $i);
	$name = "add_" . $name;
        $sql = $sql . "'" . $_POST[$name] . "'";
}
$sql = $sql . ")";
//echo $sql . "<br />\n";
if (!mysql_query($sql,$con))
  {
  die('Error: There is an error');
  }
else echo "Data saved\n";
}
else echo "<br />\n";

/* Note on PHP characters */
//http://stackoverflow.com/questions/68651/can-i-get-php-to-stop-replacing-characters-in-get-or-post-arrays






/* DELETE VALUES FROM DATABASE */
if (count($_POST)!=0 && $_POST['DeleteSelected']=='Delete Selected') {
/* !!!!!!!!!!!!!!!!!!PROTOTYPE IS CURRENTLY USING FIRST FIELD TO VALIDATE!!!!!!!!!!!!!!!!!!*/
/* !!!!!!!!!!!!!!!!!!ACTUAL WORKING PAGE SHOULD USE ID # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
$name  = mysql_field_name($result, 0);
while (list($key,$value) = each($_POST))
{
	if (substr($key,0,7)=='delete_' && $value=='Yes') 
	{
		mysql_query("DELETE FROM $tableName WHERE " . $name . "='" . substr($key,7) . "'");
	}
}
}






/* GET INFO FROM DATABASE */
echo "<form action='dbEduDe.php?data=$tableName' method='post'>\n";
$sql = "SELECT * FROM $tableName";
$result = mysql_query($sql, $con);
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}
echo "<table border='1' style='border-collapse:collapse'>\n";
/* Headers for Display Table */
echo "<tr>";
$fields = mysql_num_fields($result);
for ($i=0; $i < $fields; $i++) {
	$name  = mysql_field_name($result, $i);
	echo "<th>" . $name . "</th>";
}
echo "</tr>\n";
/* Display the data */
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>";
    for ($i=0; $i < $fields; $i++) {
 	echo "<td>" . $row[mysql_field_name($result, $i)] . "</td>";
    }
    echo "<td><input type='checkbox' name='delete_" . $row[mysql_field_name($result, 0)] . "' value='Yes' /></td>\n";
    echo "</tr>";
/* !!!!!!!!!!!!!!!!!!PROTOTYPE IS CURRENTLY USING FIRST FIELD NAME TO VALIDATE!!!!!!!!!!!!!!!!!!*/
/* !!!!!!!!!!!!!!!!!!ACTUAL WORKING PAGE SHOULD USE ID # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
}
echo "</table>\n";
echo "<input type='submit' name='DeleteSelected' value='Delete Selected' />\n";
echo "</form>\n";






// Else table doesn't exist 
} else { // END MASTER IF 


// Section to print when incorrect table names are in the URL
$sql = "SHOW TABLES FROM $mysql";
$result = mysql_query($sql);
if (!$result) {
    die("DB Error, could not list tables\n");
}
echo "<p>Select the data you wish to edit:</p>\n";
echo "<ul>\n";
while ($row = mysql_fetch_row($result)) {
    echo "<li><a title='Edit' href='dbEduDe.php?data={$row[0]}'>{$row[0]}</a></li>\n";
}
echo "</ul>\n";
}


/* CLOSE CONNECTION */
mysql_free_result($result);
mysql_close($con);

?>
</BODY>
</HTML>
